package com.jyh.config;


import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;

/**
 * 入口切面日志
 * 
 * @author lzq
 *
 */
// 描述切面类
@Aspect
@Configuration
public class AspectLogger {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 环绕通知：包围一个连接点的通知，可以在方法的调用前后完成自定义的行为，也可以选择不执行
	 * 类似Web中Servlet规范中的Filter的doFilter方法。
	 * 
	 * @param pjp
	 *            当前进程中的连接点
	 * @return
	 * @throws Throwable
	 */
	@Around("execution(* com.jyh.controller.*.*(..))")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		String strLog = "method start: " + pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName();
		logger.info("params:{}", JSONObject.toJSONString(request.getParameterMap()));
		long time = System.currentTimeMillis();
		Object retVal = pjp.proceed();
		logger.info("执行结果:{}", retVal);
		time = System.currentTimeMillis() - time;
		logger.info("耗时: {}",  time + " ms");
		strLog = "method end: " + pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName();
		logger.info(strLog);
		return retVal;
	}
}
